package org.despair2D.ui.core 
{
	import flash.display.DisplayObject;

	import org.despair2D.core.ns_despair;
	use namespace ns_despair;


final public class OriginSprite extends SpriteAdvance
{
	
	
	override public function get x() : Number { return m_x - m_originX; }
	override public function set x( v:Number ) :void
	{
		super.x = m_x = v + m_originX
	}
	
	override public function get y() : Number { return m_y - m_originY }
	override public function set y( v:Number ) :void
	{
		super.y = m_y = v + m_originY
	}
	
	
	final ns_despair function setOrigin( ox:Number, oy:Number ) : void
	{
		var tx:Number, ty:Number
		
		tx = this.x
		ty = this.y
		
		m_originX = ox
		m_originY = oy
		
		super.x = m_x = tx + m_originX
		super.y = m_y = ty + m_originY
		
		m_child.x = -m_originX
		m_child.y = -m_originY
	}
	
	
	final override ns_despair function recycle() : void
	{
		super.recycle()
		m_child = null
		cachedOriginSpriteList[cachedOriginSpriteLength++] = this
	}
	
	
	ns_despair static function getOriginSprite( child:DisplayObject ) : OriginSprite
	{
		var origin:OriginSprite = Boolean(cachedOriginSpriteLength > 0 ? cachedOriginSpriteLength-- : 0) ? cachedOriginSpriteList.pop() : new OriginSprite();
		
		origin.m_x = origin.m_y = origin.m_originX = origin.m_originY = 0
		origin.m_child = child
		origin.addChild(child)
		
		return origin
	}
	
	
	
	ns_despair static var cachedOriginSpriteList:Array = []
	
	ns_despair static var cachedOriginSpriteLength:int
	
	
	ns_despair var m_x:Number
	
	ns_despair var m_y:Number
	
	ns_despair var m_originX:Number
	
	ns_despair var m_originY:Number
	
	ns_despair var m_child:DisplayObject
	
}
}